package com.space.controller;

import com.space.entity.*;
import com.space.message.MyMessage;
import com.space.service.CityService;
import com.space.service.ExpenseService;
import com.space.service.FlightService;
import com.space.service.UserService;
import com.space.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : 港某
 * @date : 2020/2/12 15:49
 */
@Controller
@RequestMapping("space/user")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    CityService cityService;
    @Autowired
    FlightService flightService;
    @Autowired
    ExpenseService expenseService;

    @RequestMapping("userLogin")
    public String userLogin(User user, HttpServletRequest request) {
        User newUser = userService.userLogin(user);
        request.getSession().setAttribute("userLogin", newUser);
        return "redirect:index.jsp";
    }

    @RequestMapping("getAllCity")
    @ResponseBody
    public List<City> getAllCity() {
        return cityService.selectAllCity();
    }

    @RequestMapping("getHotCity")
    public String getHotCity(HttpServletRequest request) {
        List<City> allCityList = cityService.selectAllCity();
        List<City> hotCityList = cityService.selectHotCity();
        Map<String, List<Flight>> map = flightService.selectHotFlight();
        int flightCount = flightService.selectAllFlightCount();
        int userCount = userService.selectAllUserCount();
        HttpSession session = request.getSession();
        session.setAttribute("allCityList", allCityList);
        session.setAttribute("hotCityList", hotCityList);
        session.setAttribute("hotFlightMap", map);
        session.setAttribute("flightCount", flightCount);
        session.setAttribute("userCount", userCount);
        session.setAttribute("today", MyUtil.getToday());
        return "redirect:index.jsp";
    }

    @RequestMapping("checkUserInfo")
    @ResponseBody
    public AjaxResult checkUserAccount(String userAccount, String userPwd) {
        boolean confirmError = false;
        String msg = userService.checkUserInfo(userAccount, userPwd);
        if ("".equals(msg)) {
            confirmError = true;
        }
        return new AjaxResult(confirmError, msg);
    }

    @RequestMapping("checkChangePwd")
    @ResponseBody
    public AjaxResult checkChangePwd(String userAccount, String userPhone) {
        String msg = userService.checkChangePwd(userAccount, userPhone);
        if (msg != null) {
            return new AjaxResult(false, msg);
        }
        return new AjaxResult(true, "");
    }

    @RequestMapping("checkUserPhone")
    @ResponseBody
    public AjaxResult checkUserPhone(String userId, String userPhone) {
        String msg = userService.checkUserPhone(userId, userPhone);
        if (msg != null) {
            return new AjaxResult(false, msg);
        }
        return new AjaxResult(true, msg);
    }

    @RequestMapping("changeUserPwd")
    public String changeUserPwd(User user, HttpServletRequest request) {
        userService.updateUserPwd(user);
        request.getSession().invalidate();
        return "redirect:userLogin.jsp";
    }

    @RequestMapping("changeUserPhone")
    public String changeUserPhone(User user, HttpServletRequest request) {
        userService.updateUserPhone(user);
        User newUser = userService.selectUserById(user.getUserId());
        request.getSession().setAttribute("userLogin", newUser);
        return "redirect:userInfo.jsp";
    }

    @RequestMapping("userExitSystem")
    public String userExitSystem(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:userLogin.jsp";
    }

    @RequestMapping("toUpdateUserInfoJsp")
    public String toUpdateUserInfoJsp() {
        return "";
    }

    @RequestMapping("checkUserRegister")
    @ResponseBody
    public AjaxResult checkUserRegister(String userAccount, String userPhone) {
        String msg = userService.checkUserRegister(userAccount, userPhone);
        if ("".equals(msg)) {
            return new AjaxResult(true, "");
        }
        return new AjaxResult(false, msg);
    }

    @RequestMapping("userRegister")
    public String userRegister(User user) {
        userService.insertUser(user);
        return "redirect:userLogin.jsp";
    }

    @RequestMapping("searchFlight")
    public String searchFlight(String flightStartArea, String flightEndArea, String flightStartTime, ModelMap modelMap) {
        List<Flight> flightList = flightService.selectFlightByUserSearch(flightStartArea, flightEndArea, flightStartTime, false);
        List<Flight> otherFlightList = flightService.selectFlightByUserSearch(flightStartArea, flightEndArea, flightStartTime, true);
        List<Map<String, String>> mapList = flightService.selectFlightByAi(flightStartArea, flightEndArea, flightStartTime);
        modelMap.addAttribute("flightList", flightList);
        modelMap.addAttribute("otherFlightList", otherFlightList);
        modelMap.addAttribute("mapList", mapList);
        return "/user/search";
    }

    @RequestMapping("bookFlight.do")
    public String bookFlight(String flightId1, String flightId2, HttpServletRequest request, ModelMap modelMap) {
        User user = (User) request.getSession().getAttribute("userLogin");
        if (user == null) {
            modelMap.addAttribute("msg", MyMessage.LOGIN_ERROR);
            return "/user/userLogin";
        }
        flightService.bookFlight(flightId1, flightId2, user);
        return "forward:getAllExpense.do";
    }

    @RequestMapping("getAllExpense")
    public String getAllExpense(ModelMap modelMap, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("userLogin");
        if (user == null) {
            modelMap.addAttribute("msg", MyMessage.LOGIN_ERROR);
            return "/user/userLogin";
        }
        List<Expense> expenseList = expenseService.selectAllExpenseByUserId(user.getUserId());
        modelMap.addAttribute("expenseList", expenseList);
        return "/user/expense";
    }

    @RequestMapping("backTicket")
    public String backTicket(String expenseId, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("userLogin");
        if (user == null) {
            return "forward:getAllExpense.do";
        }
        if (expenseId != null) {
            expenseService.deleteExpense(Integer.parseInt(expenseId), user.getUserId());
        }
        return "forward:getAllExpense.do";
    }

    @RequestMapping("changeFlight")
    public String changeFlight(String expenseId, ModelMap modelMap, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("userLogin");
        if (user == null) {
            return "forward:getAllExpense.do";
        }
        if (MyUtil.checkNumber(expenseId)) {
            Expense expense = expenseService.selectExpenseById(Integer.parseInt(expenseId), user.getUserId());
            if (expense == null) {
                return "forward:getAllExpense.do";
            }
            List<Flight> flightList = flightService.selectSomeFlightByExpense(Integer.parseInt(expenseId), user.getUserId());
            modelMap.addAttribute("flightList", flightList);
            modelMap.addAttribute("expenseId", expenseId);
        }
        return "/user/changeFlight";
    }

    @RequestMapping("changeExpenseFlight")
    public String changeExpenseFlight(String flightId, String expenseId) {
        if (MyUtil.checkNumber(flightId) && MyUtil.checkNumber(expenseId)) {
            expenseService.updateExpenseFlightById(Integer.parseInt(expenseId), Integer.parseInt(flightId));
        }
        return "forward:getAllExpense.do";
    }

}
